package com.jet.chat.dbquery;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ChatQueryAddUser extends ChatQueryAbstract {

	@Override
	protected ChatQueryResult main(Connection conn, ChatQueryParams chatQueryParams) throws Exception {
		String username = ((ChatQueryAddUserParams) chatQueryParams).getUsername();
		String password = ((ChatQueryAddUserParams) chatQueryParams).getPassword();
		
		try {
			Statement stmt = conn.createStatement();
			ResultSet results = stmt.executeQuery("select max(id) from users");
			int id = 0;
			while(results.next()) {
				id = results.getInt(1);
			}
			id = id + 1;
			results.close();
			stmt.close();
			
			PreparedStatement ps = conn.prepareStatement("insert into users (id, username, password) values (?, ?, ?)");
			ps.setInt(1, id);
			ps.setString(2, username);
			ps.setString(3, password);
			ps.execute();
			conn.commit();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}

}
